package com.jiantong.work.impl.dao;

import com.google.common.collect.Maps;
import com.jiantong.work.model.FeedReport;
import io.terminus.common.model.Paging;
import io.terminus.common.mysql.dao.MyBatisDao;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by maowenjie on 2017/3/15.
 * 养殖端---生产中心-饲料记录--------Dao
 *
 */
@Repository
public class FeedReportDao extends MyBatisDao<FeedReport> {
    /**
     *
     * @param id 主 键
     * @return 状态
     */
    public Integer findStatusById(Long id){
        return getSqlSession().selectOne(sqlId("findStatusById"),id);
    }


    public Paging<FeedReport> pagingFeedReports(Integer offset, Integer limit, Map<String, Object> criteria) {
        if(criteria == null) {
            criteria = Maps.newHashMap();
        }
        Long total = (Long)getSqlSession().selectOne(sqlId("GOVCount"), criteria);
        if(total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<FeedReport> data = getSqlSession().selectList(sqlId("pagingGovernment"), criteria);
            return new Paging<>(total, data);
        }
    }

    /**
     * 返回当前单据的编码的最大值
     * @param number 除去流水号的前缀
     * @return string
     */
    public String findCurrentMaxNumber(String number){
        return getSqlSession().selectOne(sqlId("findCurrentMaxNumber"),number);
    }


    /**
     *根据时间段查询饲料记录
     * @param offset
     * @param limit
     * @param criteria
     * @return Paging<FeedReport>
     */

    public Paging<FeedReport> pagingbytimes (Integer offset, Integer limit, Map<String,Object> criteria){

        if (criteria == null) {
            criteria = Maps.newHashMap();
        }

        Long total = this.sqlSession.selectOne(this.sqlId("countbytimes"), criteria);
        if (total <= 0L) {
            return new Paging<>(0L, Collections.emptyList());
        } else {
            criteria.put("offset", offset);
            criteria.put("limit", limit);
            List<FeedReport> data = this.sqlSession.selectList(this.sqlId("pagingbytimes"), criteria);
            return new Paging<>(total, data);
        }


    }

    /**
     * 饲料用量求和
     * @param criteria 参数
     * @return Map<String , Object>
     */
    public Float countSumAmountFeed(Map<String , Object> criteria){
        return getSqlSession().selectOne(sqlId("countSumAmountFeed"),criteria);
    }

    public Integer countForWeb(Map<String, Object> map) {

        return getSqlSession().selectOne(sqlId("countForWeb"),map);
    }

    public List<Map<String , Object>> findSumForWeb(Map<String, Object> criteria) {

        return getSqlSession().selectList(sqlId("findSumForWeb"),criteria);
    }
}
